package online.longzipeng.householdbillssystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import online.longzipeng.householdbillssystem.business.entity.Group;
import online.longzipeng.householdbillssystem.business.vo.querey.QueryVO;
import online.longzipeng.householdbillssystem.common.exception.AppException;
import online.longzipeng.householdbillssystem.common.result.ResponseEnum;
import online.longzipeng.householdbillssystem.mapper.GroupMapper;
import online.longzipeng.householdbillssystem.service.GroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzp
 * @since 2021-04-17
 */
@Service
public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements GroupService {

    @Autowired
    GroupMapper groupMapper;
    @Autowired
    UserGroupMapper userGroupMapper;

    @Override
    public boolean add(Group group) {
        // 先判断该用户是否已经创建过了该名称的用户组
        QueryWrapper<Group> qw = new QueryWrapper<>();
        qw.eq("name",group.getName()).eq("user_id",group.getUserId());
        if (!CollectionUtils.isEmpty(groupMapper.selectList(qw))) {
            throw new AppException(ResponseEnum.GROUP_EXISTED);
        }
        return groupMapper.insert(group) > 0;
    }

    @Override
    public boolean delete(Integer id) {
        return groupMapper.deleteById(id) > 0;
    }

    @Override
    public boolean edit(Group group) {
        return groupMapper.updateById(group) > 0;
    }

    @Override
    public Group findById(Integer id) {
        return groupMapper.selectById(id);
    }

    @Override
    public List<Group> findByUserId(Integer id) {
        // 查询用户相关的所有用户组
        QueryWrapper<Group> qwGroup = new QueryWrapper<>();
        qwGroup.eq("user_id",id);
        List<Group> groups = groupMapper.selectList(qwGroup);
        // 查询用户参与的所有用户组
        QueryWrapper<UserGroup> qwUserGroup = new QueryWrapper<>();
        qwUserGroup.eq("user_id",id);
        List<UserGroup> userGroups = userGroupMapper.selectList(qwUserGroup);
        if (!CollectionUtils.isEmpty(userGroups)) {
            List<Integer> groupIds = userGroups.stream().map(UserGroup::getGroupId).collect(Collectors.toList());
            if (groups == null) {
                groups = new ArrayList<>();
            }
            for (Integer groupId : groupIds) {
                Group group = groupMapper.selectById(groupId);
                if (group!=null) {
                    groups.add(group);
                }
            }
        }
        return groups;
    }

    @Override
    public IPage<Group> getListPage(QueryVO queryVO) {
        Page<Group> groupPage = new Page<>(queryVO.getPage(),queryVO.getSize());
        return page(groupPage);
    }

}
